package com.refusea.remoting.baidu.io

import com.alibaba.fastjson2.annotation.JSONField

/**
 * @author Zhang Yan 2024/4/27
 **/
data class BaiduToken(
    @JSONField(name = "access_token")
    val accessToken: String,
    /** second **/
    @JSONField(name = "expires_in")
    val expiresIn: Long,
) {

    /** unix timestamp **/
    private val expired: Long = if (expiresIn == 0L) {
        0L
    } else {
        System.currentTimeMillis() / 1000 + expiresIn - 30L
    }

    fun valid(): Boolean {
        return expired > 0 && System.currentTimeMillis() / 1000 < expired
    }
}
